﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineManagement.IDAL;
using System.Data;
using System.Data.SqlClient;
using OnlineManagement.DBUtility;
namespace OnlineManagement.SQLServerDAL
{
    public class ProcedureHandle : IProcedureHandle
    {
        public DataSet GetPigBaseInfoProcedure(int hoggeryId)
        {
            string procName = "GetPigBaseInfo_Proce";
            SqlParameter[] paras = { new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
            paras[0].Value = hoggeryId;

            DataSet ds = DbHelperSQL.RunProcedure(procName, paras);
            return ds;
        }
        /// <summary>
        /// 根据猪购买的类型获得相关的页面初始信息
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <returns></returns>
        public DataSet GetHogInfoForPurchase(int hoggeryId) {
            string proceName = Common.PubProcedures.GetHogInfoForPurchaseProce;
            SqlParameter[] paras = { 
                                       new SqlParameter("@hoggeryID", SqlDbType.Int, 4)
                                   };
            paras[0].Value = hoggeryId;
            DataSet ds= DbHelperSQL.RunProcedure(proceName, paras);
            return ds;
        }

        public int AddPigPen(Model.PigPen model,out string returnValue)
        {
            returnValue = "不能提交空信息，请重新输入。";
            if (model == null)
            {
                return 0;
            }
            string procName = "PigPenAdd_proce";
            SqlParameter[] parameters = {
					new SqlParameter("@pigPenId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@hoggerId", SqlDbType.Int,4),
					new SqlParameter("@customizeID", SqlDbType.SmallInt,2),
					new SqlParameter("@maxCapacity", SqlDbType.TinyInt,1),
					new SqlParameter("@currentPigCount", SqlDbType.TinyInt,1),
					new SqlParameter("@currentPigType", SqlDbType.NVarChar,20),
					new SqlParameter("@currentPigBit", SqlDbType.NVarChar,20),
					new SqlParameter("@pigPenDescription", SqlDbType.NVarChar,200),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1)};
            parameters[0].Value = Guid.NewGuid();
            parameters[1].Value = model.HoggeryID;
            parameters[2].Value = model.CustomizeID;
            parameters[3].Value = model.MaxCapacity;
            parameters[4].Value = model.CurrentPigCount;
            parameters[5].Value = model.CurrentPigType;
            parameters[6].Value = model.CurrentPigBit;
            parameters[7].Value = model.PigPenDescription;
            parameters[8].Direction = ParameterDirection.Output;
            parameters[9].Direction = ParameterDirection.Output;
            int rows;
            DbHelperSQL.RunProcedure(procName, parameters, out rows, out returnValue);
           
            return rows;

        }

        /// <summary>
        /// pigs grow phase changes,包括hog 和piglets成长阶段的改变
        /// </summary>
        /// <param name="hogID"></param>
        /// <param name="pigletID"></param>
        /// <param name="phaseFromID"></param>
        /// <param name="phaseToID"></param>
        /// <param name="changedDate"></param>
        /// <param name="pigPendID"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool HogsGrowPhaseChanged(int? hogID, int? pigletID, int phaseFromID, int phaseToID, DateTime changedDate, Guid? pigPendID, out string message)
        {
            message = string.Empty;
            string proceName = Common.PubProcedures.HogsGrowPhaseChangedProce;

            SqlParameter[] parameters = {
					new SqlParameter("@HogID",  SqlDbType.Int,4),
					new SqlParameter("@PigletID", SqlDbType.Int,4),
					new SqlParameter("@PhaseFromID", SqlDbType.Int,4),
					new SqlParameter("@PhaseToID", SqlDbType.Int,4),
					new SqlParameter("@ChangedDate", SqlDbType.Date,3),
					new SqlParameter("@PigPendID", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1)};

            parameters[0].Value = hogID;
            parameters[1].Value = pigletID;
            parameters[2].Value = phaseFromID;
            parameters[3].Value = phaseToID;
            parameters[4].Value = changedDate;
            parameters[5].Value = pigPendID;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.Output;
            int rowAffected;

            DbHelperSQL.RunProcedure(proceName, parameters, out rowAffected, out message);
            
            return rowAffected>0?true:false;
        }
    }
}
